function VitalSignUtil(data){
    var that = this;

    if (data) {
        var pat = data.pat ? data.pat : data;
        that.patient = {
            code: pat.code,
            cureNo: pat.cureNo,
            name: pat.name,
            age: pat.age,
            sexText: pat.sexText,
            inTime: pat.inTime,
            deptName: pat.deptName,
            bedName: pat.bedName,
            bedName2: pat.bedName2,
            weekNo: pat.weekNo
        };
        that.dateList = that.getDateList(data.vitalSigns || pat.vitalSigns);
        that.data = that.getData(data.vitalSigns || pat.vitalSigns);
    }else{
        that.patient = {};
        that.dateList = [];
        that.data = [];
    }
}
VitalSignUtil.prototype={
    stringToDate: function(dateStr){
        if(typeof dateStr === 'object' && "getTime" in dateStr){
            dateStr.setHours(0);
            dateStr.setMinutes(0);
            dateStr.setSeconds(0);
            dateStr.setMilliseconds(0);
            return dateStr;
        }
        var seperator = "-";
        if(dateStr.indexOf("/")>0){
            seperator = "/";
        }
        var dateArr = dateStr.split(seperator);
        var year = parseInt(dateArr[0]);
        var month = parseInt(dateArr[1]);
        var day = parseInt(dateArr[2]);
        var date = new Date(year,month -1,day);
        return date;
    },
    dateDiff: function(interval,date1,date2){
        var long = date2.getTime() - date1.getTime();
        switch(interval.toLowerCase()){
            case "y": return parseInt(date2.getFullYear()-date1.getFullYear());break;
            case "m": return parseInt((date2.getFullYear()-date1.getFullYear())*12+(date2.getMonth()-date1.getMonth()));break;
            case "d": return parseInt(long/1000/60/60/24);break;
            case "w": return parseInt(long/1000/60/60/24/7);break;
            case "h": return parseInt(long/1000/60/60);break;
            case "mi": return parseInt(long/1000/60);break;
            case "s": return parseInt(long/1000);
            case "ms": return parseInt(long);
        }
    },
    getDateList: function(data){
        var result = [];
        if(!data || data.length<=0){
            return result;
        }
        for(var i=0;i<data.length;i++){
            var item=data[i];
            if(!item || !item.opDate){
                continue;
            }
            var opDate =item.opDate.substring(0,10);
            if(result.indexOf(opDate) == -1){
                result.push(opDate);
            }
        }
        return result.sort();
    },
    getStartDate: function(){
        var that = this;
        var dateList = that.dateList;
        if(!dateList || dataList.length<=0){
            return;
        }
        return dateList[0];
    },
    getH: function(opTime){
        var h = 0;
        switch (opTime) {
            case 2:
                h = 1;
                break;
            case 6:
                h = 2;
                break;
            case 10:
                h = 3;
                break;
            case 14:
                h = 4;
                break;
            case 18:
                h = 5;
                break;
            case 22:
                h = 6;
                break;
        }
        return h;
    },
    getData: function(data){
        var that = this;
        var result = [];
        if(!data || data.length <=0){
          return result;
        }
        if(!that.dateList || that.dateList.length<=0){
            return result;
        }
        for(var i=0;i<data.length;i++){
            var item = data[i];
            if(!item.opDate || !item.opTime){
              continue;
            }
            result.push({
                //days: that.dateDiff('d',that.startDate, that.stringToDate(item.opDate)),
                days: that.dateList.indexOf(item.opDate.substring(0,10)),
                h: that.getH(item.opTime),
                bodyHeat: item.bodyHeat,
                bodyHeatDrop: item.bodyHeatDrop,
                reCheck: item.reCheck,
                heatType: item.heatType,
                pulse: item.pulse,
                heartRate: item.heartRate,
                breathe: item.breathe,
                bloodPressure: item.bloodPressure,
                remark: item.remark,
                remark2: item.remark2,
                inAll: item.inAll,
                outShit: item.outShit,
                outPee: item.outPee,
                outOther: item.outOther,
                weight: item.weight,
                skinTest: item.skinTest,
                other: item.other
            })
        }
        return result;
    },
    getBodyHeat: function(type){
        var that = this;
        var data = that.data;
        if(!data || data.length<=0){
            return;
        }
        var result = [];
        for(var i=0;i<data.length;i++){
            var item = data[i];
            if(typeof item.days ==='undefined' || !item.bodyHeat || !item.h){
                continue;
            }
            if(item.heatType == type) {
                result.push({
                    days: item.days,
                    h: item.h,
                    bodyHeat: item.bodyHeat
                })
            }
        }
        return result;
    },
    getBloodPressure: function(){
        var that = this;
        var data = that.data;
        if(!data || data.length<=0){
            return;
        }
        var result = [];
        var dayList = [];
        for(var i=0;i<data.length;i++){
            var item = data[i];
            if(typeof item.days ==='undefined' || !item.h || !item.bloodPressure){
                continue;
            }
            if(dayList.indexOf(item.days) == -1) {
                result.push({
                    days: item.days,
                    h: item.h,
                    bloodPressure: item.bloodPressure
                });
                dayList.push(item.days);
            }
        }
        return result;
    },
    getWeight: function(){
        var that = this;
        var data = that.data;
        if(!data || data.length<=0){
            return;
        }
        var result = [];
        var dayList = [];
        for(var i=0;i<data.length;i++){
            var item = data[i];
            if(typeof item.days ==='undefined' || !item.h || !item.weight){
                continue;
            }
            if(dayList.indexOf(item.days) == -1) {
                result.push({
                    days: item.days,
                    h: item.h,
                    weight: item.weight
                });
                dayList.push(item.days);
            }
        }
        return result;
    },
    doDraw: function(p) {
        var that = this;
        var line = function (x1, y1, x2, y2) {
            return 'M' + x1 + ' ' + y1 + ' L' + x2 + ' ' + y2;
        };
        var days = that.dateList.length <= 7 ? 7 : that.dateList.length; //天数
        var HCell = 22, //单元格高度
            WCell = 20, //单元格宽度
            cellTopPad = 5, //单元格上边距
            pulseCols = 3, //脉搏列数
            heatCols = 2, //体温列数
            leftCols = pulseCols + heatCols,//左边总列数
            dayCols = 6, //一天占的列数
            rightCols = days * dayCols, //右边7天占的总列数
            cols = leftCols + rightCols, //总行数
            headRows = 4, //头部行数
            bodyRows = 43, //中部行数
            footRows = 12, //底部行数
            pulseRows = 5, //脉搏一格行数
            heatRows = 5, //体温一格行数
            rows = headRows + bodyRows + footRows, //总行数
            hxRows = headRows + bodyRows,//呼吸行数
            dbRows = hxRows + 1,//大便行数
            chuRows = dbRows + 1,//出量行数
            ruRows = chuRows + 5,//入量行数
            tbY = 0; //y坐标初始值
        var zr = zrender.init(document.getElementById(p.elem), {renderer: 'canvas'});
        zr.resize({width: cols * WCell + 20, height: 1414});
        var grp = new zrender.Group();
        var draw = layui.zdraw.init(zr, grp);
        draw.initHoverText();

        //画表格头行
        tbY = (function (y) {
            var pat=that.patient || {};
            draw.hText({str: "博罗县妇幼保健计划生育服务中心", fontSize: 23, dx: 260, dy: y});
            y += HCell * 1.4;
            draw.hText({str: "体  温  单", fontSize: 23, dx: 400, dy: y});
            y += HCell * 1.4;
            draw.hText({str: "姓名:"+(pat.name||''), fontSize: 14, dx: 0, dy: y});
            draw.hText({str: "性别:"+(pat.sexText||''), fontSize: 14, dx: 110, dy: y});
            draw.hText({str: "年龄:"+(pat.age||''), fontSize: 14, dx: 190, dy: y});
            draw.hText({str: "入院日期:"+(pat.inTime||'').substring(0,10), fontSize: 14, dx: 290, dy: y});
            draw.hText({str: "病区:"+(pat.deptName||''), fontSize: 14, dx: 450, dy: y});
            draw.hText({str: "床号:"+(pat.bedName2||''), fontSize: 14, dx: 600, dy: y});
            draw.hText({str: "住院号:"+(pat.code||''), fontSize: 14, dx: 700, dy: y});
            if(pat.weekNo){
                draw.hText({str: "第"+(pat.weekNo||'')+"周", fontSize: 14, dx: 850, dy: y});
            }
            y += 20;
            return y;
        })(tbY);

        //黑色线
        (function (y) {
            var points = [];
            //顶部横线
            var y1 = y, y2 = y;
            var x1 = 0, x2 = cols * WCell;
            for (var i = 0; i < headRows + 1; i++) {
                points.push(line(x1, y1, x2, y1));
                y1 += HCell;
            }
            //中部横线
            x1 = leftCols * WCell;
            y1 = y + HCell * (headRows + 1);
            x2 = cols * WCell;
            for (var i = -1; i < bodyRows; i++) {  //-1代表42上面的线
                if (i % heatRows != 0) { //标尺线将另用粗线画
                    points.push(line(x1, y1, x2, y1));
                }
                y1 += HCell;
            }
            //底部横线
            x1 = 0;
            x2 = cols * WCell;
            y1 = y + (headRows + bodyRows) * HCell;
            for (var i = -1; i < footRows; i++) { //-1: 多一条边线
                if (i == 2 || i == 3 || i == 4 || i == 5) {
                    x1 = WCell;
                } else {
                    x1 = 0;
                }
                points.push(line(x1, y1, x2, y1));
                y1 += HCell;
            }
            //底部竖线
            x1 = WCell;
            y1 = y + (headRows + bodyRows + 2) * HCell;
            y2 = y1 + 5 * HCell;
            points.push(line(x1, y1, x1, y2));

            //中部竖线
            x1 = (leftCols + 1) * WCell;
            y1 = y + (headRows - 1) * HCell;
            y2 = y + (headRows + bodyRows+1) * HCell;
            for (var i = 1; i <= rightCols; i++) {
                if (i % dayCols != 0) {  //每天的分割线将另用粗线画
                    points.push(line(x1, y1, x1, y2));
                }
                x1 += WCell;
            }
            //3条竖边线
            //1.最左边
            x1 = 0;
            y2 = y + HCell * rows;
            y1 = y;
            points.push(line(x1, y1, x1, y2));

            //2.体温右边线
            x1 = leftCols * WCell;
            points.push(line(x1, y1, x1, y2));

            //3.最右边
            x1 = cols * WCell;
            points.push(line(x1, y1, x1, y2));

            //脉搏和体温分割线
            x1 = pulseCols * WCell;
            y1 = y + HCell * (headRows - 1);
            y2 = y + HCell * (headRows + bodyRows);
            points.push(line(x1, y1, x1, y2));

            draw.path({str: points.join(' ') + ' Z'});
        })(tbY);

        //红色竖线
        (function (y) {
            var points = [];
            var x1 = (leftCols + dayCols) * WCell;
            var y1 = y, y2 = y + HCell * rows;
            var max = days - 1;
            for (var i = 0; i < max; i++) {
                points.push(line(x1, y1, x1, y2));
                x1 += dayCols * WCell;
            }
            draw.path({str: points.join(' ') + ' Z', stroke: 'red'});
        })(tbY);

        //中部标度粗横线
        (function (y) {
            var points = [];
            var x1 = WCell * leftCols, y1 = y + HCell * (headRows + 2), x2 = cols * WCell;
            for (var i = 0; i < 9; i++) {
                points.push(line(x1, y1, x2, y1));
                y1 += HCell * heatRows;
            }
            draw.path({str: points.join(' ') + ' Z', stroke: 'blue', lineWidth: 2});
        })(tbY);

        //住院日期
        (function (y, dateList) {
            var x1 = leftCols * WCell / 2 - WCell * 1.4, y1 = y + cellTopPad;
            draw.hText({str: '住院日期', dx: x1, dy: y1});
            x1 = leftCols * WCell + WCell;
            for (var i = 0; i < dateList.length; i++) {
                draw.hText({str: dateList[i], dx: x1, dy: y1});
                x1 += dayCols * WCell;
            }
            // x1 = leftCols*WCell+WCell;
            // draw.hText({str:inDate,dx:x1,dy:y1});
            // var tmpDate = tools.stringToDate(inDate);
            // x1 = (leftCols+dayCols+dayCols/2)*WCell-WCell/4;
            // var max=days-1;
            // for(var i=0;i<max;i++){
            //     var d = tools.dateAdd(tmpDate,'d',1).getDate();
            //     draw.hText({str:d,dx:x1,dy:y1});
            //     x1 += dayCols*WCell;
            // }
        })(tbY, that.dateList);

        //住院天数
        (function (y) {
            var x1 = leftCols * WCell / 2 - WCell * 1.4, y1 = y + cellTopPad + HCell;
            draw.hText({str: '住院天数', dx: x1, dy: y1});
            x1 = (leftCols + dayCols / 2) * WCell - WCell / 4;
            for (var i = 1; i <= days; i++) {
                draw.hText({str: i, dx: x1, dy: y1});
                x1 += dayCols * WCell;
            }
        })(tbY);

        //时间(上午/下午)
        (function (y) {
            var x1 = leftCols * WCell / 2 - WCell, y1 = y + cellTopPad + HCell * 2;
            draw.hText({str: '时间', dx: x1, dy: y1});
            x1 += leftCols * WCell + WCell / 3;
            for (var i = 1; i <= days; i++) {
                draw.hText({str: '上午', dx: x1, dy: y1});
                draw.hText({str: '下午', dx: x1 + WCell * 2.3, dy: y1});
                x1 += dayCols * WCell;
            }
        })(tbY);

        //表头脉搏体温/小时
        (function (y) {
            var x1 = leftCols * WCell / 2 - WCell * 1.6, y1 = y + cellTopPad + HCell * 3;
            draw.hText({str: '脉搏', dx: x1, dy: y1});
            draw.hText({str: '体温', dx: pulseCols * WCell + 5, dy: y1});
            x1 = (leftCols) * WCell + 6;
            var w = WCell;
            for (var i = 1; i <= days; i++) {
                draw.hText({str: '2', dx: x1, dy: y1});
                draw.hText({str: '6', dx: x1 + w, dy: y1});
                draw.hText({str: '10', dx: x1 + w * 1.8, dy: y1});
                draw.hText({str: '2', dx: x1 + w * 3, dy: y1});
                draw.hText({str: '6', dx: x1 + w * 4, dy: y1});
                draw.hText({str: '10', dx: x1 + w * 4.8, dy: y1});
                x1 += dayCols * WCell;
            }
        })(tbY);

        //体温标尺
        (function (y) {
            var x1 = WCell * pulseCols + WCell / 2, y1 = y + HCell * (headRows + 1) + HCell / 2 + 2;
            draw.hText({str: '(℃)', dx: x1, dy: y + HCell * headRows + HCell / 3});
            for (var i = 42; i >= 34; i--) {
                draw.hText({str: i + '°', dx: x1, dy: y1});
                y1 += HCell * heatRows;
            }
        })(tbY);

        //脉搏标度
        (function (y) {
            var x1 = WCell * 1.5, y1 = y + HCell * (headRows + 1) + HCell / 2 + 2;
            draw.hText({str: '次/分', dx: x1 - 5, dy: y + HCell * headRows + HCell / 3, fontSize: 13});
            for (var i = 160; i >= 20; i -= 20) {
                draw.hText({str: i, dx: x1, dy: y1});
                y1 += HCell * pulseRows;
            }
        })(tbY);

        //呼吸数
        (function (y) {
            var x1 = WCell + 5, y1 = y + HCell * hxRows + cellTopPad;
            draw.hText({str: '呼吸数', dx: x1, dy: y1});
        })(tbY);

        //大便次数
        (function (y) {
            var x1 = WCell + 5, y1 = y + HCell * dbRows + cellTopPad;
            draw.hText({str: '大便次数', dx: x1, dy: y1});
        })(tbY);

        //出量
        (function (y) {
            var x1 = WCell / 2 - 8, y1 = y + HCell * (chuRows + 1) + cellTopPad;
            draw.hText({str: '出', dx: x1, dy: y1});
            y1 += HCell;
            draw.hText({str: '量', dx: x1, dy: y1});
        })(tbY);

        //尿量
        (function (y) {
            var x1 = WCell * 1.5, y1 = y + HCell * (chuRows) + cellTopPad;
            draw.hText({str: '尿量ml', dx: x1, dy: y1});
        })(tbY);

        //痰量
        (function (y) {
            var x1 = WCell * 1.5, y1 = y + HCell * (chuRows + 1) + cellTopPad;
            draw.hText({str: '痰量ml', dx: x1, dy: y1});
        })(tbY);
        //引流量
        (function (y) {
            var x1 = WCell * 1.5, y1 = y + HCell * (chuRows + 2) + cellTopPad;
            draw.hText({str: '引流量ml', dx: x1, dy: y1, fontSize: 12});
        })(tbY);
        //呕吐量
        (function (y) {
            var x1 = WCell * 1.5, y1 = y + HCell * (chuRows + 3) + cellTopPad;
            draw.hText({str: '呕吐量ml', dx: x1, dy: y1, fontSize: 12});
        })(tbY);
        //总量
        (function (y) {
            var x1 = WCell * 1.5, y1 = y + HCell * (chuRows + 4) + cellTopPad;
            draw.hText({str: '总量ml', dx: x1, dy: y1});
        })(tbY);
        //入量
        (function (y) {
            var x1 = WCell * 0.8, y1 = y + HCell * (ruRows) + cellTopPad;
            draw.hText({str: '入量ml', dx: x1, dy: y1});
        })(tbY);
        //血压
        (function (y) {
            var x1 = WCell * 0.8, y1 = y + HCell * (ruRows + 1) + cellTopPad;
            draw.hText({str: '血压mmHg', dx: x1, dy: y1});
        })(tbY);
        //体重
        (function (y) {
            var x1 = WCell * 0.8, y1 = y + HCell * (ruRows + 2) + cellTopPad;
            draw.hText({str: '体重kg', dx: x1, dy: y1});
        })(tbY);
        //手术后天数
        (function (y) {
            var x1 = WCell * 0.8, y1 = y + HCell * (ruRows + 3) + cellTopPad;
            draw.hText({str: '手术后天数', dx: x1, dy: y1});
        })(tbY);
        //分娩后天数
        (function (y) {
            var x1 = WCell * 0.8, y1 = y + HCell * (ruRows + 4) + cellTopPad;
            draw.hText({str: '分娩后天数', dx: x1, dy: y1});
        })(tbY);

        //画体温函数
        var drawHeat = function (p) {
            var heats = that.getBodyHeat(p.type);
            if (!heats || heats.length <= 0) {
                return;
            }
            var points = [];
            var base = 34, baseY = p.y + (headRows + bodyRows - 1) * HCell;
            for (var i = 0; i < heats.length; i++) {
                var item = heats[i];
                var x1 = leftCols * WCell + item.days * dayCols * WCell + (item.h - 1) * WCell + WCell / 2,
                    y1 = baseY + (base - item.bodyHeat) * (heatRows * HCell);
                points.push([x1, y1, item.bodyHeat]);
            }
            draw.polyLine({points: points, color: p.color});
            for (var i = 0; i < points.length; i++) {
                var t = points[i];
                if (p.type == 0) { //口温
                    draw.hollowCircle({x: t[0], y: t[1], color: p.color, txt: t[2] + '°'});
                } else if (p.type == 1) { //腋温
                    draw.drawX({x: t[0], y: t[1], color: p.color, txt: t[2] + '°'});
                } else if (p.type == 2) { //肛温
                    draw.plusCircle({x: t[0], y: t[1], color: p.color, txt: t[2] + '°'});
                }
            }
        };
        var myColors = {
            kwColor: '#CC00FF', //口温颜色
            ywColor: '#0000FF', //腋温颜色
            gwColor: '#660033', //肛温颜色
            mbColor: '#FF0000' //脉搏颜色
        };

        //画各种体温线
        (function(y){
            drawHeat({type:0,y:y,color:myColors.kwColor}); //口温
            drawHeat({type:1,y:y,color:myColors.ywColor}); //腋温
            drawHeat({type:2,y:y,color:myColors.gwColor}); //肛温
        })(tbY);

        //画脉搏线
        (function(p){
            if(!that.data || that.data.length<=0){
                return;
            }
            var points = [];
            var base=0,baseY =p.y+(headRows+bodyRows-1)*HCell;
            for(var i=0;i<that.data.length;i++){
                var item = that.data[i];
                if(!item.pulse || item.pulse<=0){
                    continue;
                }
                var x1=leftCols*WCell+item.days*dayCols*WCell+(item.h-1)*WCell+WCell/2,
                    y1=baseY + (base-item.pulse)*(pulseRows*HCell)/20;
                points.push([x1,y1,item.pulse]);
            }
            draw.polyLine({points:points,color:p.color});
            for(var i=0;i<points.length;i++){
                var t = points[i];
                draw.circel({x: t[0], y: t[1], color: p.color, txt: t[2]});
            }
        })({y:tbY,color:myColors.mbColor});

        //画备注信息
        (function(y){
            var x1=10,y1=y+(headRows+bodyRows-heatRows)*HCell;
            draw.hText({str:'口温',dx:x1,dy:y1});
            draw.hollowCircle({x: x1+40, y: y1+5, color: myColors.kwColor});
            y1+=HCell;
            draw.hText({str:'腋温',dx:x1,dy:y1});
            draw.drawX({x: x1+40, y: y1+5, color: myColors.ywColor});
            y1+=HCell;
            draw.hText({str:'肛温',dx:x1,dy:y1});
            draw.plusCircle({x: x1+40, y: y1+5, color: myColors.gwColor});
            y1+=HCell;
            draw.hText({str:'脉搏',dx:x1,dy:y1});
            draw.circel({x: x1+40, y: y1+5, color: myColors.mbColor});
        })(tbY);

        //画其他数据
        (function(y){
            if(!that.data){
                return;
            }
            for(var i=0;i<that.data.length;i++){
                var item=that.data[i];
                if(!item || !item.h){
                    continue;
                }
                var x1 = leftCols * WCell + item.days * dayCols * WCell + (item.h - 1) * WCell + 1,
                    y1;
                //呼吸
                y1 = y + HCell * hxRows + cellTopPad;
                if(!!item.breathe) {
                    draw.hText({str: item.breathe, dx: x1, dy: y1});
                }
                //大便次数
                y1 = y + HCell * dbRows + cellTopPad;
                if(!!item.outShit && item.outShit != '0'){
                    draw.hText({str: item.outShit, dx: x1, dy: y1});
                }
                //小便次数
                y1 = y + HCell * chuRows + cellTopPad;
                if(!!item.outPee && item.outPee != '0'){
                    draw.hText({str: item.outPee, dx: x1, dy: y1});
                }
                //入量
                y1 = y + HCell * (ruRows) + cellTopPad;
                if(!!item.inAll && item.inAll != '0'){
                    draw.hText({str: item.inAll, dx: x1, dy: y1});
                }
                //备注
                var remark = (function(){
                    var rs = item.remark||'';
                    if(rs.length<=0){
                        rs = item.remark2||'';
                    }else{
                        if((item.remark2||'').length>0){
                            rs += ','+item.remark2;
                        }
                    }
                    return rs;
                })();
                if(remark.length>0){
                    y1 = y + HCell*(headRows+0.5);
                    draw.vText({str: remark, dx: x1+2, dy: y1});
                }
            }
            //一天只显示一天数据
            //血压
            var bloods = that.getBloodPressure();
            if(!!bloods) {
                for(var i=0;i<bloods.length;i++) {
                    var item = bloods[i];
                    var x1 = leftCols * WCell + item.days * dayCols * WCell + (2) * WCell + 1,
                        y1 = y + HCell * (ruRows + 1) + cellTopPad;
                    draw.hText({str: item.bloodPressure, dx: x1, dy: y1});
                }
            }
            //体重, 一天只显示条数据
            var weights = that.getWeight();
            if(!!weights) {
                for(var i=0;i<weights.length;i++) {
                    var item = weights[i];
                    var x1 = leftCols * WCell + item.days * dayCols * WCell + (2) * WCell + 1,
                        y1 = y + HCell * (ruRows + 2) + cellTopPad;
                    draw.hText({str: item.weight, dx: x1, dy: y1});
                }
            }
        })(tbY);

        grp.attr('position', [p.x, p.y]);
        zr.add(grp);
    }
};
